/*******************************************************************
This file is part of iLaunch.

iLaunch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iLaunch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iLaunch.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

#include <stdafx.h>
//#include <string.h>

#include "resourceppc.h"
#include "CSettings.h"
#include "Macros.h"
#include "RegistryUtils.h"
#include "FileUtils.h"

CSettings::CSettings(void) {
	TCHAR buffer[MAX_PATH] = {0};
	TCHAR szLanguagePath[MAX_PATH] = {0};

    //////////////////////////////////////////////////
    // Settings

	LoadSetting(buffer, REGISTRY_MAXLENGTH, SZ_ILAUNCH_REG_KEY,
        INI_EXIT_ON_MIN_KEY, INI_EXIT_ON_MIN_DEFAULT);
    this->doExitOnMinimize = '1' == buffer[0];

	LoadSetting(buffer, REGISTRY_MAXLENGTH, SZ_ILAUNCH_REG_KEY,
        INI_EXIT_ON_ACTION_KEY, INI_EXIT_ON_ACTION_DEFAULT);
    this->doExitOnAction = '1' == buffer[0];

    LoadSetting(buffer, REGISTRY_MAXLENGTH, SZ_ILAUNCH_REG_KEY,
        INI_FAST_GFX_KEY, INI_FAST_GFX_DEFAULT);
    this->doFastGraphics = '1' == buffer[0];

	LoadSetting(buffer, REGISTRY_MAXLENGTH, SZ_ILAUNCH_REG_KEY,
        INI_FULLSCREEN_KEY, INI_FULLSCREEN_DEFAULT);
    this->doShowFullScreen = '1' == buffer[0];

	LoadSetting(buffer, REGISTRY_MAXLENGTH, SZ_ILAUNCH_REG_KEY,
		INI_ENABLE_SENSOR_KEY, INI_ENABLE_SENSOR_DEFAULT);
	this->doEnableSensor = '1' == buffer[0];

	LoadSetting(buffer, REGISTRY_MAXLENGTH, SZ_ILAUNCH_REG_KEY,
		INI_GRAVITY_KEY, INI_GRAVITY_DEFAULT);
	this->gravity = min(100, max(-100, _ttol(buffer))) / (float)100.0;

    LoadSetting(buffer, MAX_PATH, SZ_ILAUNCH_REG_KEY,
        INI_SKIN_KEY, INI_SKIN_DEFAULT);
	if (buffer[0] == '\\') 
		StringCchCopy(this->skin_path, MAX_PATH, buffer);
	else
	    GetCurDirFilename(this->skin_path, buffer, TEXT("png"));

	// use the default skin anyway, if the specified skin can't be found
	if (!FileExists(this->skin_path)) {
		GetCurDirFilename(this->skin_path, INI_SKIN_DEFAULT, TEXT("png"));

		// die if the skin _still_ can't be found
		if (!FileExists(this->skin_path)) {
			HWND hWnd = FindWindow (SZ_APP_NAME, NULL);
			MessageBox(hWnd, TEXT("No skin found"), SZ_APP_NAME, 0);
			PostMessage(hWnd, WM_CLOSE, 0, 0);
		}
	}


    LoadSetting(buffer, MAX_PATH, SZ_ILAUNCH_REG_KEY,
        INI_LANGUAGE_KEY, INI_LANGUAGE_DEFAULT);
	if (buffer[0] == '\\') 
		StringCchCopy(szLanguagePath, MAX_PATH, buffer);
	else
	    GetCurDirFilename(szLanguagePath, buffer, TEXT("lng"));

    //////////////////////////////////////////////////
    // Language
    const struct LanguageSetting languageSettings[] = {
        &this->alphabet, TEXT("alphabet"), TEXT(""),
		&this->office_string, TEXT("office"), TEXT("Office Mobile"),
		&this->multimedia_string, TEXT("multimedia"), TEXT("Multimedia"),
		&this->applications_string, TEXT("applications"), TEXT("Applications"),
		&this->connections_string, TEXT("connections"), TEXT("Connections"),
		&this->system_string, TEXT("system"), TEXT("System Tools"),
    };

	// Read the file into this->language_data TCHAR array
    HANDLE hCache = CreateFile(szLanguagePath, GENERIC_READ, 0, NULL, 
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	SetFilePointer(hCache, 0, NULL, FILE_BEGIN);

	char cbuffer[2048];
    DWORD dwFileSize = min(2048, GetFileSize(hCache, NULL));
    DWORD dwBytesRead;

	BOOL readOk = ReadFile(hCache, cbuffer, dwFileSize, &dwBytesRead, NULL);

	if (!readOk) {
		// TODO: report error?
		for (int i = 0; i < ARRAYSIZE(languageSettings); i++) {
			*(languageSettings[i].ppSetting) = languageSettings[i].pszDefault;
		}
	}
	else {
		ASSERT(dwFileSize == dwBytesRead);
		::mbstowcs(this->language_data, cbuffer, 2048);

		// Then set all the TCHAR * to the appropriate locations in that array
		TCHAR * pStr = 0;
		for (int i = 0; i < ARRAYSIZE(languageSettings); i++) {

			pStr = _tcsstr(this->language_data, languageSettings[i].pszName);

			if (pStr == NULL) {
				*(languageSettings[i].ppSetting) = languageSettings[i].pszDefault;
				continue;
			}

			// we want what's after the equal, not what's before it
			*(languageSettings[i].ppSetting) = _tcsstr(pStr, TEXT("=")) + 1;
		}

		// Then add nulls in the appropriate locations 
		// (replace '=', '\r', '\n', with 0)
		// (the other data in the file is then just junk)
		for (pStr = this->language_data; pStr < this->language_data + 2048; pStr++) {
			if (*pStr == '\r' || *pStr == '\n' || *pStr == '=')
				*pStr = 0;
		}
	}

	////////////////////////////////////////////////////
	//// Special handling for "Favorites" category:

	//// 1. If they've chosen a category specifically, it's in the registry
	//LoadSetting(this->favorite_category, REGISTRY_MAXLENGTH, SZ_ICONTACT_REG_KEY,
	//	INI_FAVORITE_CAT_KEY, NULL);

	//// 2. Otherwise, use the default category from the language file
	//// (or "Favorites" if it doesn't exist in the language file)
	//if (0 == _tcslen(this->favorite_category))
	//	StringCchCopy(this->favorite_category, REGISTRY_MAXLENGTH,
	//	this->favorites_default);
}
